<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="等待处理的查询申请" name="first">
        <el-table :data="applyList" border fit highlight-current-row>
          <el-table-column type="expand" label="条件详情" width="80">
            <template slot-scope="scope">
              <condition-form :condition="scope.row.condition"></condition-form>
            </template>
          </el-table-column>
          <el-table-column prop="id" label="查询单号" />
          <el-table-column prop="applyTime" label="申请时间" />
          <el-table-column label="成功率" width="100">
            <template slot-scope="scope">
                <el-tag v-show="scope.row.result <= 0.5" type="danger">{{ parseFloat(scope.row.result * 100).toFixed(2) }}
                  %</el-tag>
                <el-tag v-show="scope.row.result > 0.5" type="success">{{ parseFloat(scope.row.result * 100).toFixed(2) }}
                  %</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="productName" label="数据来源" />
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-button v-show="scope.row.result === 0.0" type="primary" size="mini"
                @click="fresh(scope.row.id)">重新检查</el-button>
              <el-button v-show="scope.row.result !== 0.0" type="primary" size="mini" @click="pass(scope.row.id)">通过</el-button>
              <el-button v-show="scope.row.result !== 0.0" type="danger" size="mini" @click="deny(scope.row.id)">拒绝</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination :current-page="applyPage.apage" :page-size="applyPage.alimit" :total="applyPage.atotal"
          :page-sizes="[5, 10, 20, 50]" style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper" @current-change="handleListCurrent"
          @size-change="handleListSize" />
      </el-tab-pane>
      <el-tab-pane label="已完成的申请" name="second">
        <el-table :data="historyList" border fit highlight-current-row>
          <el-table-column type="expand" label="条件详情" width="80">
            <template slot-scope="scope">
              <condition-form :condition="scope.row.condition"></condition-form>
            </template>
          </el-table-column>
          <el-table-column prop="id" label="查询单号" />
          <el-table-column prop="applyTime" label="申请时间" />
          <el-table-column prop="operateTime" label="处理时间" />
          <el-table-column label="状态" width="80">
            <template slot-scope="scope">
              <el-tag v-show="scope.row.state === -1" type="danger">拒绝</el-tag>
              <el-tag v-show="scope.row.state === 1" type="success">通过</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="analyzeTime" label="分析时间" />
          <el-table-column label="成功率" width="100">
            <template slot-scope="scope">
                <el-tag v-show="scope.row.result <= 0.5" type="danger">{{ parseFloat(scope.row.result * 100).toFixed(2) }}
                  %</el-tag>
                <el-tag v-show="scope.row.result > 0.5" type="success">{{ parseFloat(scope.row.result * 100).toFixed(2) }}
                  %</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination :current-page="historyPage.hpage" :page-size="historyPage.hlimit" :total="historyPage.htotal"
          :page-sizes="[5, 10, 20, 50]" style="padding: 30px 0; text-align: center;"
          layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange"
          @size-change="handleSizeChange" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import conditionForm from '@/components/conditionQueryForm'
import conditionQuery from '@/api/conditionQuery.js'

export default {
  components: {
    conditionForm,
  },
  data() {
    return {
      activeName: 'first',
      applyList: null,
      applypage: {
        page: 1,
        limit: 5,
        total: 0
      },
      historyList: null,
      historyPage: {
        page: 1,
        limit: 5,
        total: 0
      },
    }
  },
  created() {
    this.applypage = {
      apage: 1,
      alimit: 5,
      atotal: 0
    }
    this.historyPage = {
      hpage: 1,
      hlimit: 5,
      htotal: 0
    }
  },
  mounted() {
    if (this.activeName === 'first') {
      this.getList();
    }
    else if (this.activeName === 'second') {
      this.getHistory();
    }
  },
  methods: {
    // 获得待处理的申请列表
    getList() {
      const page = this.applypage.apage
      const limit = this.applypage.alimit
      //向后台获得要处理的申请列表 
      conditionQuery.getExamine(page, limit)
        .then(response => {
          //对列表的每一项将后台保存的条件json字符串转回json
          response.data.item.map(item => {
            item.condition = JSON.parse(item.conditionJson);
          });
          this.applyList = response.data.item
          this.applyPage.atotal = response.data.total
        })
        .catch(() => {
        })
    },
    // 获得自己处理过的申请记录
    getHistory() {
      const page = this.historyPage.hpage
      const limit = this.historyPage.hlimit
      //向后台查询自己已经处理的申请列表 
      conditionQuery.getApprove(page, limit)
        .then(response => {
          //对列表的每一项将后台保存的条件json字符串转回json
          response.data.item.map(item => {
            item.condition = JSON.parse(item.conditionJson);
          });
          this.historyList = response.data.item
          this.historyPage.atotal = response.data.total
        })
        .catch(() => {
        })
    },
    // 通过该申请
    pass(id) {
      conditionQuery.pass(id)
        .then(response => {
          this.$message({
            message: '处理成功',
            type: 'success'
          })
          this.getList()
        })
    },
    //重新刷新成功率
    fresh(id) {
      conditionQuery.refresh(id)
        .then(response => {
          this.$message({
            message: '刷新成功',
            type: 'success'
          })
          //刷新列表
          this.getHistory()
        })
    },
    // 拒绝该申请
    deny(id) {
      //弹出对话框，只有点击确定才会向后台发拒绝消息
      this.$confirm('拒绝该条请求吗，拒绝后将无法修改状态', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        //后台允许选择多条，这里只支持单条
        let arr = [];
        arr.push(id);
        //向后台发送拒绝请求
        conditionQuery.deny(arr)
          .then(response => {
            this.$message({
              message: '处理成功',
              type: 'success'
            })
            //刷新列表
            this.getList()
          })
      }).catch(() => { })
    },
    // 分页
    handleListCurrent(val) {
      this.applyPage.apage = val
      this.getList()
    },
    handleListSize(val) {
      this.applyPage.alimit = val
      this.applyPage.apage = 1
      this.getList()
    },
    // 分页
    handleCurrentChange(val) {
      this.historyPage.hpage = val
      this.getHistory()
    },
    handleSizeChange(val) {
      this.historyPage.hlimit = val
      this.historyPage.hpage = 1
      this.getHistory()
    },
    //点击tab页切换列表
    handleClick(val) {
      if (val.name === 'first') {
        this.getList()
      } else {
        this.getHistory()
      }
    },
  }
}
</script>
